package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.List;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.db.CmsPointsName;
import com.gesee.zyspps.db.CmsPointsNameDao.Properties;
import com.gesee.zyspps.utils.StringUtil;
import com.gesee.zyspps.utils.ToastUtil;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class AddLeibieActivity extends BaseActivity
{
    
    private Spinner spinner;
    private EditText subCode,subName,parCode,parName,colorValue,et_mai,remark;
    private Button enter,cancle;
    private ImageView come_back;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_leibie);
        initView();  
    }

    private void initView()
    {
        // TODO Auto-generated method stub
        come_back = (ImageView) findViewById(R.id.come_back);
        come_back.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                AddLeibieActivity.this.finish();
            }
        });
        
        subCode = (EditText) findViewById(R.id.subCode);
        subName = (EditText) findViewById(R.id.subName);
        parCode = (EditText) findViewById(R.id.parCode);
        parName = (EditText) findViewById(R.id.parName);
        colorValue = (EditText) findViewById(R.id.colorValue);
        et_mai = (EditText) findViewById(R.id.et_mai);
        remark = (EditText) findViewById(R.id.remark);
        
        enter = (Button) findViewById(R.id.enter);
        cancle = (Button) findViewById(R.id.cancle);
        
        spinner = (Spinner) findViewById(R.id.mai);
        
        List list = new ArrayList();
        list.add("1:管顶");
        list.add("2:管中");
        list.add("3:管底");
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        spinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                if(position == 0){
                    et_mai.setText("管顶");
                }
                if(position == 1){
                    et_mai.setText("管中");
                }
                if(position == 2){
                    et_mai.setText("管底");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        cancle.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                AddLeibieActivity.this.finish();
            }
        });
        
        enter.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                List<CmsPointsName> list = ZSApplication.getDaoSession("zyspps-db").getCmsPointsNameDao().queryBuilder().orderDesc(Properties.Sort).list();
                CmsPointsName cms = new CmsPointsName();
                //cms.setId(id);
                cms.setSubCode(subCode.getText().toString().trim());
                cms.setSubName(subName.getText().toString().trim());
                cms.setParCode(parCode.getText().toString().trim());
                cms.setParName(parName.getText().toString().trim());
                cms.setPointsColor(colorValue.getText().toString().trim());
                cms.setRemark(remark.getText().toString().trim());
                cms.setBuryType(et_mai.getText().toString().trim());
                if(list!=null&&list.size()>0){
                    cms.setSort(list.get(0).getSort()+1);
                }else{
                    cms.setSort(1);
                }
                if(StringUtil.isEmpty(subCode.getText().toString().trim())||
                        StringUtil.isEmpty(subName.getText().toString().trim())||
                        StringUtil.isEmpty(parCode.getText().toString().trim())||
                        StringUtil.isEmpty(parName.getText().toString().trim())||
                        StringUtil.isEmpty(colorValue.getText().toString().trim())||
                        StringUtil.isEmpty(et_mai.getText().toString().trim())){
                    ToastUtil.show(AddLeibieActivity.this, "输入框必须填写!");
                }else{
                    ZSApplication.getDaoSession("zyspps-db").getCmsPointsNameDao().insert(cms);
                    AddLeibieActivity.this.finish();
                }
                
            }
        });

        
    }
}
